package com.billing.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.billing.model.dao.CrmContactMapper;
import com.billing.model.dao.CrmCustomerMapper;
import com.billing.model.dao.CrmLinkmanMapper;
import com.billing.model.dao.CrmUserUploadCardMapper;
import com.billing.model.entity.CrmContact;
import com.billing.model.entity.CrmCustomer;
import com.billing.model.entity.CrmLinkman;
import com.billing.model.entity.CrmUserUploadCard;
import com.billing.service.CrmService;
import com.billing.service.ManagerPermissionsService;
@Service
public class CrmServiceImpl implements CrmService {
	@Resource
	private CrmCustomerMapper crmCustomerMapper;
	@Resource
	private CrmLinkmanMapper crmLinkmanMapper;
	@Resource
	private CrmContactMapper crmContactMapper;
	@Resource
	private CrmUserUploadCardMapper crmUserUploadCardMapper;
	@Resource
	private ManagerPermissionsService managerService;
	@Override
	public Map<String, Object> getCustomerPage(String userid, String name,String type, int pageSize, int currentPage) {
		int totalNum = crmCustomerMapper.selectCustomerNum(name,type,userid);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<CrmCustomer> list=crmCustomerMapper.selectCustomerPage(name, userid, (currentPage - 1) * pageSize, pageSize,type);
		Map<String, Object> map = new HashMap<>();
		map.put("totalNum", totalNum);
		map.put("totalPage", totalPage);
		map.put("total", totalPage);
		map.put("list", list);
		return map;
	}
	@Override
	public int addCustomer(CrmCustomer customer) {
		return crmCustomerMapper.insertSelective(customer);
	}
	@Override
	public int updateCustomer(CrmCustomer customer) {
		return crmCustomerMapper.updateByPrimaryKeySelective(customer);
	}
	@Override
	public CrmCustomer getCustomerById(String id) {
		return crmCustomerMapper.selectByPrimaryKey(Integer.parseInt(id));
	}
	@Override
	public List<CrmLinkman> getlinkmanByCustomerid(String customerid) {
		return crmLinkmanMapper.selectByCustomerid(customerid);
	}
	@Override
	public Map<String, Object> getlinkmanPageByCustomerid(String customerid,int pageSize,int currentPage) {
		int totalNum = crmLinkmanMapper.getNum(customerid);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<CrmLinkman> list=crmLinkmanMapper.getPage(customerid, (currentPage - 1) * pageSize, pageSize);
		//客户公司名
		String customerName = crmCustomerMapper.selectByPrimaryKey(Integer.parseInt(customerid)).getCustName();
		Map<String, Object> map = new HashMap<>();
		map.put("customerName", customerName);
		map.put("totalNum", totalNum);
		map.put("totalPage", totalPage);
		map.put("total", totalPage);
		map.put("currentPage", currentPage);
		map.put("list", list);
		return map;
	}
	@Override
	public CrmLinkman getlinkmanByid(String linkmanid) {
		return crmLinkmanMapper.selectByPrimaryKey(Integer.parseInt(linkmanid));
	}
	@Override
	public int addlinkman(CrmLinkman linkman) {
		return crmLinkmanMapper.insertSelective(linkman);
	}
	@Override
	public int updatelinkman(CrmLinkman linkman) {
		return crmLinkmanMapper.updateByPrimaryKeySelective(linkman);
	}
	@Override
	public List<CrmCustomer> getCustomerByUserid(String userid) {
		return crmCustomerMapper.selectCustomerByUserid(userid);
	}
	@Override
	public Map<String, Object> getContactPage(String userid, String custId, int pageSize, int currentPage) {
		int totalNum = crmContactMapper.selectNum(userid, custId);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<CrmContact> list=crmContactMapper.selectPage(userid, custId, (currentPage - 1) * pageSize, pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("totalNum", totalNum);
		map.put("totalPage", totalPage);
		map.put("total", totalPage);
		map.put("currentPage", currentPage);
		map.put("list", list);
		return map;
	}
	@Override
	public void addContact(CrmContact contact) {
		crmContactMapper.insertSelective(contact);
		//修改客户状态为跟进中
		CrmCustomer customer = new CrmCustomer();
		customer.setId(contact.getCustId());
		customer.setStatus(1);
		crmCustomerMapper.updateByPrimaryKeySelective(customer);
	}
	@Override
	public CrmContact getContactById(String id) {
		return crmContactMapper.selectByPrimaryKey(Integer.parseInt(id));
	}
	@Override
	public List<CrmCustomer> getCustomerByUseridNoBracket(String userid) {
		return crmCustomerMapper.selectCustomerByUseridNoBracket(userid);
	}
	@Override
	public List<CrmContact> getCrmContactByLkmid(String lkmId) {
		return crmContactMapper.selectContactByLkmid(lkmId);
	}
	@Override
	public CrmCustomer getCustomerByNameAndUserid(String custName, String userid) {
		return crmCustomerMapper.selectByNameAndUserid(custName, userid);
	}
	@Override
	public boolean customerIsBracket(String custId) {
		if(crmCustomerMapper.selectByPrimaryKey(Integer.parseInt(custId)).getStatus() == 3){
			return true;
		}
		return false;
	}
	@Override
	public boolean customerIsNameExist(String custName, String userid, Integer custId) {
		CrmCustomer customer = crmCustomerMapper.selectByNameAndUserid(custName, userid);
		if(customer != null){
			if(customer.getId() == custId){
				return false;
			}else {
				return true;
			}
		}
		return false;
	}
	@Override
	public int countCustomerByMonth(String userid,String month) {
		return crmCustomerMapper.countCustomerByMonthAndUserid(month, userid);
	}
	@Override
	public int countCustContactByMonth(String userid, String month) {
		return crmContactMapper.countCustContactByMonthAndUserid(month, userid);
	}
	@Override
	public Map<String,Object> getCrmContactByLkmidPage(String lkmId, int pageSize, int currentPage) {
		int totalNum = crmContactMapper.selectNumBylkmid(lkmId);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<CrmContact> list=crmContactMapper.selectPageByLkmid(lkmId, (currentPage - 1) * pageSize, pageSize);
		Map<String, Object> map = new HashMap<>();
		map.put("totalNum", totalNum);
		map.put("totalPage", totalPage);
		map.put("total", totalPage);
		map.put("currentPage", currentPage);
		map.put("list", list);
		return map;
	}
	@Override
	public int addCardUpload(CrmUserUploadCard uploadCard) {
		return crmUserUploadCardMapper.insertSelective(uploadCard);
	}
	@Override
	public List<CrmContact> getContactByCust(String custId) {
		return crmContactMapper.selectContactByCust(custId);
	}
	@Override
	public Map<String, Object> selectByKeyword(String keyword) {
		return crmCustomerMapper.selectByKeyword(keyword);
	}
	@Override
	public Map<String, Object> selectAllCustomer(int userid, String name, String type, int pageSize,
			int currentPage) {
		int totalNum = crmCustomerMapper.selectAllCustomerNum(name,type,userid);
		int totalPage = totalNum / pageSize;
		if (totalNum % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<CrmCustomer> list=crmCustomerMapper.selectAllCustomer(name, userid, (currentPage - 1) * pageSize, pageSize,type);
		Map<String, Object> map = new HashMap<>();
		map.put("totalNum", totalNum);
		map.put("totalPage", totalPage);
		map.put("total", totalPage);
		map.put("list", list);
		return map;
	}
}
